package com.anlu.kafka.hellokafka;

import com.anlu.kafka.config.BusiConst;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import javax.naming.ldap.BasicControl;
import java.util.Properties;

/**
 * kafka的生产者
 */
public class HelloKafkaProducer {

    public static void main(String[] args) {
        Properties properties = new Properties();
        //设置属性
        properties.put("bootstrap.servers","127.0.0.1:9092");
        properties.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);
        ProducerRecord<String,String> record;
        try{
            record=new ProducerRecord<String, String>(BusiConst.HELLO_TOPIC,"teacher02","anlu");

            producer.send(record);
            System.out.println("message is sent");

        }catch (Exception e){
            producer.close();
        }

    }
}
